package com.ucomm.talent.activity;

import java.util.ArrayList;
import java.util.Date;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.*;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.HotCompanyActivity.ListItemListener;
import com.ucomm.talent.adapter.HotCompanyListAdapter;
import com.ucomm.talent.common.DebugUtil;
import com.ucomm.talent.manager.PostDataToNet;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.HotCompanyInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.vita.bduck.http.AjaxParams;
/**
 * 用户界面
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-5-21
 * To change this template use File | Settings | File Templates.
 */
public class UsersActivity extends BaseActivity implements OnClickListener {
    private static final int LOADING = 0x001;
    private static final int LOAD_DISMISS = 0x002;
    private static final int LOAD_USER_INFO = 0x003;
    private static final int LOGIN_FINISH = 0x004;


    private CustomProgressDialog customProgressDialog;
    private Button loginButton = null;//登陆按钮
    private TextView tvRegisterNow = null;

    private TextView userEdit;
    private TextView userLogout;

    private LayoutInflater layoutInflater;
    private ViewFlipper viewFlipper;
    private String userName = null;
    private String password = null;
    private PostDataToNet postDataToNet;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.users_layout);
//        mHandler = new Handler(this);
        
        initView();
        
    }
    
    

    private void initView() {
        customProgressDialog = new CustomProgressDialog(this);

        layoutInflater = LayoutInflater.from(this);
        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper_layout);
        this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
        this.viewFlipper.setFlipInterval(3000);
        //如果没有登录
        this.viewFlipper.addView(layoutInflaterLogin());

    }

    private View layoutInflaterLogin() {
        LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.login_layout, null);
        loginButton = (Button) linearLayout.findViewById(R.id.loginButton);
        loginButton.setOnClickListener(this);
//        EditText userNameView = (EditText) linearLayout.findViewById(R.id.username);
//        EditText passwordView = (EditText) linearLayout.findViewById(R.id.password);
        
        tvRegisterNow = (TextView) linearLayout.findViewById(R.id.tv_register_now);
        tvRegisterNow.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);


        return linearLayout;
    }

    private View layoutInflaterUserInfo() {
        ScrollView scrollView = (ScrollView) layoutInflater.inflate(R.layout.personal_center_layout, null);
        userEdit = (TextView) scrollView.findViewById(R.id.user_edit);
        userLogout = (TextView) scrollView.findViewById(R.id.user_logout);

        userEdit.setOnClickListener(this);
        userLogout.setOnClickListener(this);

        return scrollView;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == loginButton.getId()) {//登陆
        	
//        	searchManager.searchDataFromNetwrok(AppConstants.TEST);
        	EditText userNameView = (EditText) layoutInflaterLogin().findViewById(R.id.username);
        	EditText passwordView = (EditText) layoutInflaterLogin().findViewById(R.id.password);
        	userName = userNameView.getText().toString();
            password = passwordView.getText().toString();
        	AjaxParams params = new AjaxParams();
        	params.put("userName", userName);
        	params.put("password", password);
        	postDataToNet = new PostDataToNet(this, mHandler);
        	//上传数据
        	postDataToNet.postDataToNetwork(AppConstants.LOGIN, params);
        	
            mHandler.sendEmptyMessage(LOADING);

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewFlipper.addView(layoutInflaterUserInfo());
                    mHandler.sendEmptyMessage(LOGIN_FINISH);
                }
            }, 2000);
        }else if(id == userEdit.getId()){

        }else if(id == userLogout.getId()){
            viewFlipper.showPrevious();
            viewFlipper.removeViewAt(1);

            //注销用户数据
            //TODO
        }

    }

    /**
     * 监听返回
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean flag = true;
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i("vita", "-----onKeyDown");
            mHandler.sendEmptyMessage(LOAD_DISMISS);
        }
        return flag;
    }
    
    Handler mHandler = new Handler(){
    	
    	@Override
    	public void handleMessage(Message msg) {
    		int what = msg.what;
    		DebugUtil.Log(UsersActivity.this,"---------- " + customProgressDialog.toString());
    		switch (what){
    		case AppConstants.MsgStatus.MSG_ERROR:
    			if (customProgressDialog != null && customProgressDialog.isShowing())
    				customProgressDialog.dismiss();
    			((AppException)msg.obj).makeToast(UsersActivity.this);
    			break;
    		case AppConstants.MsgStatus.MSG_SUCCUSS:
    			if (customProgressDialog != null && customProgressDialog.isShowing())
    				customProgressDialog.dismiss();
    			
    			break;
    		case AppConstants.MsgStatus.MSG_LOADING:
    			if (customProgressDialog != null && !customProgressDialog.isShowing())
    				customProgressDialog.show();
    			break;
    		default:
    			break;
    		}
    		super.handleMessage(msg);
    	}
    };
    


//    @Override
//    public boolean handleMessage(Message message) {
//        int what = message.what;
//        switch (what) {
//            case LOADING:
//                if (customProgressDialog != null) {
//                    customProgressDialog.show();
//                }
//                break;
//            case LOGIN_FINISH:
//                viewFlipper.showNext();
//            case LOAD_DISMISS:
//                if (customProgressDialog != null && customProgressDialog.isShowing()) {
//                    customProgressDialog.dismiss();
//                }
//                break;
//            case LOAD_USER_INFO:
//                break;
//
//            default:
//                break;
//        }
//        return true;
//    }
}

